<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>时间差</title>
</head>

<body>

    <script>
        // var time1 = new Date(2019, 7, 27, 15, 22, 34)
        var time1 = new Date()
        var time2 = new Date(2019, 7, 31, 21, 00, 00)

        function subTime(t1, t2) {
            var str = ''
            //1,让两个时间戳相减 得到两个时间相差多少秒钟
            var t = parseInt(Math.abs(t1.getTime() - t2.getTime()) / 1000)
            //算多少天
            var day = parseInt(t / (60 * 60 * 24))
            console.log(day + "天");
            //算多少小时
            //总的时间差的秒-有多少天(也要重新换算成秒)
            var subDay = t - day * 60 * 60 * 24
            var hours = parseInt(subDay / (60 * 60))
            console.log(hours + "小时");
            //算多少分钟
            var subMinute = subDay - hours * 60 * 60
            var minutes = parseInt(subMinute / 60)
            console.log(minutes);
            //算多少秒钟
            var seconds = t % 60
            console.log(seconds);

            str += day + "天 "
            str += hours + "小时 "
            str += minutes + "分钟 "
            str += seconds + "秒"

            console.log(str);
            return str
        }

        subTime(time1, time2)
    </script>
</body>

</html>